@extends('admins.layouts.home')
 

@section('sidebar')
    @parent
    @if(!empty($categoryList))
    <ul class="list-group">
        <li class="list-group-item active">文章管理</li>
        @foreach ($categoryList as $rows)
            <li class="list-group-item">
                <a href="/admins/posts?category={{ $rows['category_name_en'] }}">
                    <strong> {{ $rows['category_name'] }} </strong>
                    <span class="badge">{{ isset($categoryCount[$rows['category_name_en']]) ? $categoryCount[$rows['name_en']] : '' }}</span></a>
            </li>
            
        @endforeach
    </ul>
    @endif

@endsection

@section('content')

<ol class="breadcrumb">
  <li><a href="/">首页</a></li>
  <li><a href="/article/all" class="active">文章列表</a></li>
</ol>

<div class="main">
    <div class="panel panel-default">
        <div class="panel-heading">
        筛选
        </div>
      <div class="panel-body">
        Basic panel example
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
         文章列表 <a href="/admins/books/add" class="btn btn-xs btn-success">  新增教程 </a>
      </div>
      <div class="panel-body">

        
        @if ($listData['data'])

            <table class="table">
                <tr>
                  <th>编号</th>
                  <th>标题</th>
                  <th>分类</th>
                  <th>时间</th>
                  <th>状态</th>
                  <th>操作</th>
                </tr>
            @foreach ($listData['data'] as $rows)
                @if($rows['status'] == 1) 
                    <tr class="">
                @elseif($rows['status'] == 2) 
                    <tr class="">
                @elseif($rows['status'] == 0)
                    <tr class="active">
                @else 
                    <tr class="">
                @endif
                  <td scope="row">{{ $rows['id'] }}</td>
                  <td>
                  <a href="/admins/books/show-{{ $rows['id'] }}"> {{ $rows['book_name'] }} </a> </td>
                  </td>
                  <td> {{$rows['category']}} </td>
                  <td>
                    <small> {{ $rows['updated_at'] ?: '--' }} </small> 
                  </td>
                  <td>
                    @if($rows['status'] == 1) <span class="label label-success">正常</span>
                    @elseif($rows['status'] == 2) <span class="label label-success">正常</span>
                    @elseif($rows['status'] == 0) <span class="label label-danger">未发布</span>
                    @else <span class="label label-default">未知</span>
                    @endif
                  </td>
                  <td>
                        <form id="form_{{$rows['id']}}">
                            <input type="hidden" name="id" value="{{$rows['id']}}" />
                            <input type="hidden" name="action" value="updateStatus" />
                            <input type="hidden" name="currentStatus" value="{{$rows['status']}}" />
                       
                            @if($rows['status'] == 1) 
                                <input type="hidden" name="targetStatus" value="0" />
                                <button data-id="{{ $rows['id'] }}" type="button" class="updateStatus btn btn-xs btn-default"> 取消发布 </button>
                            @elseif($rows['status'] == 0) 
                                <input type="hidden" name="targetStatus" value="1" />
                                <button data-id="{{ $rows['id'] }}" type="button" class="updateStatus btn btn-xs btn-success"> 马上发布 </button>
                            @else
                            @endif
                       </form>
                      <a href="/admins/books/edit/{{ $rows['id'] }}">Edit</a>
                  </td>
                </tr>
 
            @endforeach
            </table>

        @endif
        </div>
    </div>
</div>
 
    <!-- 显示分页 -->
    <nav aria-label="...">
        <ul class="pager">
            @if ($listData['prev_page_url'])
                <li class="previous"><a href="{{$listData['prev_page_url']}}">上一页 <span aria-hidden="true">&larr;</span></a></li>
            @else
                <li class="previous disabled"><a href="#">上一页 <span aria-hidden="true">&larr;</span></a></li>
            @endif

            @if ($listData['next_page_url'])
                <li class="next"><a href="{{$listData['next_page_url']}}">下一页 <span aria-hidden="true">&rarr;</span></a></li>
            @else
                <li class="next disabled"><a href="#">下一页 <span aria-hidden="true">&rarr;</span></a></li>
            @endif


        </ul>
    </nav>

    <script type="application/javascript">

    function ajaxPost(ajaxUrl, params) {
        var Request = jQuery.post( ajaxUrl, params, 'json');
        Request.done(function(data) {
            console.log( "second success" );
            console.log( data );
        });
        Request.fail(function(XMLHttpRequest) {
            console.log( "ajax post error: " + XMLHttpRequest.status );
        });
        Request.always(function() {
            //console.log( "finished" );
        });
        return Request;
    }

        var requestUrl = '/admins/books/ajax';
        // Specify a function to execute when the DOM is fully loaded.
        $( document ).ready(function() {
            
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            //页面加载完毕，结束进度条
            // TopProgressBar.finish();

            $(".updateStatus").click(function(){

                var id = $(this).attr('data-id');
                var params = $('#form_'+id).serialize();
                ajaxPost(requestUrl, params);
                return ;

                $.ajax({
                    url: requestUrl,
                    method: 'POST',
                    data: dataParams,
                    dataType:'json',
                    success: function(result) {

                        if( result.code == 1 ){
                            window.location.href = result.data.url;
                        } else {
                            alert(result.msg);
                            window.location.reload();
                        }
                        // window.location.reload();
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("操作失败，请稍后重试！");
                        console.log(XMLHttpRequest.status);
                        console.log(XMLHttpRequest.readyState);
                        console.log(textStatus);
                    }
                });
                //$("#myDiv").html(htmlobj.responseText);
            });

        });

    </script>

@endsection